<?php

namespace App\Http\ApiControllers;

use App\Http\Resources\PlayerIndexResource;
use App\Http\Resources\PlayerResource;
use App\Jobs\MakePlayerPoster;
use App\Models\Player;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class PlayerController extends ApiController
{
    public function index(Player $player): JsonResponse
    {
        $player->increment('clicks');

        return $this->success(new PlayerIndexResource($player));
    }

    public function info(Player $player): JsonResponse
    {
        $player->increment('clicks');

        return $this->success(new PlayerResource($player));
    }

    /**
     * Notes   : 获取选手海报
     *
     * @Date   : 2024/7/29 下午5:52
     * @Author : <Jason.C>
     * @param  Request  $request
     * @param  Player  $player
     * @return JsonResponse
     */
    public function share(Request $request, Player $player): JsonResponse
    {
        $poster = $player->posters()->latest()->first();

        if (!$poster) {
            $poster = $player->posters()->create([
                'application_id' => $request->application->id,
            ]);

            MakePlayerPoster::dispatchSync($player, $poster);
        }

        return $this->success([
            'poster' => $poster?->cover_url,
        ]);
    }
}
